package mamikeji.liaowa.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.Image;
import mamikeji.liaowa.bean.MyCircleBean;
import mamikeji.liaowa.ui.activity.circle.PreviewCircleImgActivity;
import mamikeji.liaowa.widget.ninegrid.CustomImageView;
import mamikeji.liaowa.widget.ninegrid.NineGridlayout;
import mamikeji.liaowa.widget.ninegrid.ScreenTools;

/**
 * Created by 廉冲-android开发 on 2017/5/17.
 */
public class MyCircleAdapter extends BaseAdapter {
    List<MyCircleBean.DataEntity> mList = new ArrayList<>();
    LayoutInflater inflater;
    Context context;
    OnItemClickListener mOnItemClickListener;
    ViewHolder viewHolder;
    public boolean bool_delete = false;

    public MyCircleAdapter() {
    }

    public MyCircleAdapter(List<MyCircleBean.DataEntity> mList, Context context) {
        this.mList = mList;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onZanClick(int fc_id, int position);

        void onDeleteClick(int fc_id, int position);
    }

    public void setList(List<MyCircleBean.DataEntity> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_my_circle, null);
            viewHolder = new ViewHolder();

            viewHolder.textView_my_circle_creattime_sel = (TextView) convertView.findViewById(R.id.textView_my_circle_creattime_sel);
            viewHolder.textView_my_circle_recommend_sel = (TextView) convertView.findViewById(R.id.textView_my_circle_recommend_sel);
            viewHolder.textView_my_circle_zannum_sel = (TextView) convertView.findViewById(R.id.textView_my_circle_zannum_sel);
            viewHolder.textView_my_circle_content = (TextView) convertView.findViewById(R.id.textView_my_circle_content);
            viewHolder.textView_my_circle_time = (TextView) convertView.findViewById(R.id.textView_my_circle_time);

            viewHolder.imageView_my_circle = (ImageView) convertView.findViewById(R.id.imageView_my_circle);
            viewHolder.imageView_my_circle_zan_sel = (ImageView) convertView.findViewById(R.id.imageView_my_circle_zan_sel);

            viewHolder.iv_ngrid_layout_my_circle = (NineGridlayout) convertView.findViewById(R.id.iv_ngrid_layout_my_circle);
            viewHolder.iv_oneimage_my_circle = (CustomImageView) convertView.findViewById(R.id.iv_oneimage_my_circle);

            viewHolder.relativeLayout_my_circle_delete = (RelativeLayout) convertView.findViewById(R.id.relativeLayout_my_circle_delete);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final MyCircleBean.DataEntity dataEntity = mList.get(position);
        String[] split2 = new String[]{};
        if (!dataEntity.getImgurl().equals("") && !dataEntity.getImgurl().equals("null") && dataEntity.getImgurl() != null) {
            split2 = dataEntity.getImgurl().split(",");
        }

        viewHolder.imageView_my_circle_zan_sel.setImageResource(dataEntity.getPraisestate() == 1 ? R.drawable.icon_dianzan_h : R.drawable.icon_dianzan_n);
        viewHolder.textView_my_circle_recommend_sel.setText(dataEntity.getViewnumber() + "");
        viewHolder.textView_my_circle_zannum_sel.setText(dataEntity.getPraisenumber() + "");

        if (dataEntity.getContent() == null || dataEntity.getContent().equals("")) {
            viewHolder.textView_my_circle_content.setVisibility(View.GONE);
        } else {
            viewHolder.textView_my_circle_content.setVisibility(View.VISIBLE);
            viewHolder.textView_my_circle_content.setText(dataEntity.getContent());

            if (dataEntity.getContent().length() >= 20) {
                viewHolder.textView_my_circle_content.setLines(2);
                viewHolder.textView_my_circle_content.setEllipsize(TextUtils.TruncateAt.END);
            }
        }

        mistiming(position);

        if (split2.length == 0) {
            viewHolder.iv_ngrid_layout_my_circle.setVisibility(View.GONE);
            viewHolder.iv_oneimage_my_circle.setVisibility(View.GONE);
        } else if (split2.length == 1) {
            viewHolder.iv_ngrid_layout_my_circle.setVisibility(View.GONE);
            viewHolder.iv_oneimage_my_circle.setVisibility(View.VISIBLE);

            handlerOneImage(viewHolder, new Image(split2[0], 250, 250));
        } else {
            viewHolder.iv_ngrid_layout_my_circle.setVisibility(View.VISIBLE);
            viewHolder.iv_oneimage_my_circle.setVisibility(View.GONE);

            List<String> lsists = new ArrayList<>();
            for (int i = 0; i < split2.length; i++) {
                lsists.add(split2[i]);
            }

            viewHolder.iv_ngrid_layout_my_circle.setImagesData(lsists);
        }

        final String[] finalSplit = split2;
        viewHolder.iv_ngrid_layout_my_circle.setOnItemClickListener(new NineGridlayout.OnItemClickListener() {
            @Override
            public void onMultipleImgClick(int position) {
                if (!bool_delete) {
                    ArrayList<String> stringList = new ArrayList<String>();
                    for (int i = 0; i < finalSplit.length; i++) {
                        stringList.add(finalSplit[i]);
                    }

                    Intent intent = new Intent(context, PreviewCircleImgActivity.class);
                    intent.putStringArrayListExtra("urlList", stringList);
                    intent.putExtra("position", position);
                    context.startActivity(intent);
                }
            }
        });
        viewHolder.iv_oneimage_my_circle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!bool_delete) {
                    ArrayList<String> stringList = new ArrayList<String>();
                    for (int i = 0; i < finalSplit.length; i++) {
                        stringList.add(finalSplit[i]);
                    }

                    Intent intent = new Intent(context, PreviewCircleImgActivity.class);
                    intent.putStringArrayListExtra("urlList", stringList);
                    intent.putExtra("position", 1);
                    context.startActivity(intent);
                }
            }
        });

        viewHolder.imageView_my_circle_zan_sel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!bool_delete) {
                    mOnItemClickListener.onZanClick(dataEntity.getFcid(), position);
                }
            }
        });

        viewHolder.relativeLayout_my_circle_delete.setVisibility(bool_delete ? View.VISIBLE : View.GONE);

        viewHolder.relativeLayout_my_circle_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onDeleteClick(dataEntity.getFcid(), position);
            }
        });
        return convertView;
    }

    public class ViewHolder {
        TextView textView_my_circle_time;
        TextView textView_my_circle_content;
        TextView textView_my_circle_creattime_sel;
        TextView textView_my_circle_recommend_sel;
        TextView textView_my_circle_zannum_sel;
        ImageView imageView_my_circle;
        NineGridlayout iv_ngrid_layout_my_circle;
        CustomImageView iv_oneimage_my_circle;

        ImageView imageView_my_circle_zan_sel;

        RelativeLayout relativeLayout_my_circle_delete;
    }

    //判断时间差：新闻发布时间与当前系统时间的差值：如果小于60分钟换算成分钟；大于60分钟，换算成小时；大于24小时，换算成天
    public void mistiming(int position) {
        //获取当前时间
        String date = new SimpleDateFormat("yyyy/MM/dd hh:mm").format(new Date());
        String[] mTimeSplit = date.split(" ");
        String[] date_yMd = mTimeSplit[0].split("/");

        //获取本条数据发布时间
        String[] mEntitySplit = mList.get(position).getPublisdate().split(" ");
        String[] entity_yMd = mEntitySplit[0].split("/");

        //设置朋友圈 时：分
        String[] entity_date_hm = mEntitySplit[1].split(":");
        viewHolder.textView_my_circle_creattime_sel.setText(entity_date_hm[0] + "：" + entity_date_hm[1]);

        if (position == 0) {
            viewHolder.textView_my_circle_time.setVisibility(View.VISIBLE);
            viewHolder.imageView_my_circle.setVisibility(View.GONE);

            if (Integer.parseInt(date_yMd[0]) == Integer.parseInt(entity_yMd[0]) &&
                    Integer.parseInt(date_yMd[1]) == Integer.parseInt(entity_yMd[1]) &&
                    Integer.parseInt(date_yMd[2]) == Integer.parseInt(entity_yMd[2])) {
                viewHolder.textView_my_circle_time.setText("今天");
            } else {
                String[] entity_time_yMd = mEntitySplit[0].split("/");
                String date_create = entity_time_yMd[2] + getM(entity_time_yMd[1]);
                viewHolder.textView_my_circle_time.setText(date_create);
            }
        } else {
            String[] mLastTimeSplit = mList.get(position - 1).getPublisdate().split(" ");
            String[] date_Last_yMd = mLastTimeSplit[0].split("/");

            if (Integer.parseInt(date_Last_yMd[0]) == Integer.parseInt(entity_yMd[0]) &&
                    Integer.parseInt(date_Last_yMd[1]) == Integer.parseInt(entity_yMd[1]) &&
                    Integer.parseInt(date_Last_yMd[2]) == Integer.parseInt(entity_yMd[2])) {
                viewHolder.textView_my_circle_time.setVisibility(View.GONE);
                viewHolder.imageView_my_circle.setVisibility(View.VISIBLE);
            } else {
                viewHolder.textView_my_circle_time.setVisibility(View.VISIBLE);
                viewHolder.imageView_my_circle.setVisibility(View.GONE);

                String date_create = entity_yMd[2] + getM(entity_yMd[1]);
                viewHolder.textView_my_circle_time.setText(date_create);
            }
        }
    }

    private String getM(String num) {
        String date_create = "";
        switch (num) {
            case "1":
                date_create = "一月";
                break;
            case "2":
                date_create = "二月";
                break;
            case "3":
                date_create = "三月";
                break;
            case "4":
                date_create = "四月";
                break;
            case "5":
                date_create = "五月";
                break;
            case "6":
                date_create = "六月";
                break;
            case "7":
                date_create = "七月";
                break;
            case "8":
                date_create = "八月";
                break;
            case "9":
                date_create = "九月";
                break;
            case "10":
                date_create = "十月";
                break;
            case "11":
                date_create = "十一月";
                break;
            case "12":
                date_create = "十二月";
                break;
        }
        return date_create;
    }

    private void handlerOneImage(ViewHolder viewHolder, Image image) {
        int totalWidth;
        int imageWidth;
        int imageHeight;
        ScreenTools screentools = ScreenTools.instance(context);
        totalWidth = screentools.getScreenWidth() - screentools.dip2px(80);
        imageWidth = screentools.dip2px(image.getWidth());
        imageHeight = screentools.dip2px(image.getHeight());
        if (image.getWidth() <= image.getHeight()) {
            if (imageHeight > totalWidth) {
                imageHeight = totalWidth;
                imageWidth = (imageHeight * image.getWidth()) / image.getHeight();
            }
        } else {
            if (imageWidth > totalWidth) {
                imageWidth = totalWidth;
                imageHeight = (imageWidth * image.getHeight()) / image.getWidth();
            }
        }
        ViewGroup.LayoutParams layoutparams = viewHolder.iv_oneimage_my_circle.getLayoutParams();
        layoutparams.height = imageHeight;
        layoutparams.width = imageWidth;
        viewHolder.iv_oneimage_my_circle.setLayoutParams(layoutparams);
        viewHolder.iv_oneimage_my_circle.setClickable(true);
        viewHolder.iv_oneimage_my_circle.setScaleType(ImageView.ScaleType.FIT_XY);
        viewHolder.iv_oneimage_my_circle.setImageUrl(image.getUrl());

    }
}
